package com.aigao.golf.management.user.controller.organization;

import com.aigao.golf.Utils.DaoUtil;
import com.aigao.golf.common.base.PageController;
import com.aigao.golf.common.permissions.PermissOpt;
import com.aigao.golf.common.permissions.PermissionType;
import com.aigao.golf.common.permissions.ReqPermission;
import com.aigao.golf.common.result.ResultEnum;
import com.aigao.golf.common.result.ResultTo;
import com.aigao.golf.management.user.model.CmOrg;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author QiangHe
 * @date 2018/1/11 21:34
 */
@RestController
@RequestMapping("/group/unit")
public class GroupController extends PageController<CmOrg> {
    /**
     * 按管理员查询下属单位
     *
     * @param creator
     * @return
     */
    @ReqPermission(type = PermissionType.GROUP_INFO, opt = PermissOpt.READ)
    @PostMapping("/list")
    public ResultTo list(@RequestParam("creator") String creator, Integer pageStart, Integer pageLength, String pageOrder) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("creator", creator);
        param.put("flag", 0);
        return dataPage(pageStart, pageLength, pageOrder, param);
    }

    /**
     * 按父单位查询下属单位
     *
     * @return
     */
    @ReqPermission(type = PermissionType.GROUP_INFO, opt = PermissOpt.READ)
    @PostMapping("/sub")
    public ResultTo listSub(@RequestParam("org_id") String parentId) {
        Example example = new Example(CmOrg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parentId)
                .andNotEqualTo("flag", 1);
        return new ResultTo(adaptService.selectByExample(example));
    }

    /**
     * 添加下属机构
     *
     * @return
     */
    @ReqPermission(type = PermissionType.GROUP_INFO, opt = PermissOpt.WRITE)
    @PostMapping("/add")
    public ResultTo add(String name, String creator,
                        @RequestParam("parent_id") String parentId) {
        CmOrg org = new CmOrg();
        org.setCreator(creator);
        org.setName(name);
        org.setParentId(parentId);
        org.setCreateTime(new Date());
        if (adaptService.insertSelective(org) == 1) {
            return new ResultTo(org);
        } else {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "保存失败");
        }
    }

    @ReqPermission(type = PermissionType.GROUP_INFO, opt = PermissOpt.UPDATE)
    @PostMapping("/update")
    public ResultTo update(String id,
                           @RequestParam(name = "name", required = false) String name,
                           @RequestParam(name = "parent_id", required = false) String parentId) {
        CmOrg org = new CmOrg();
        org.setId(id);
        org.setName(name);
        org.setParentId(parentId);
        org.setCreateTime(new Date());
        if (name != null || parentId != null) {
            org.setUpdateTime(new Date());
        }
        Example example = new Example(CmOrg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        if (adaptService.updateByExampleSelective(org, example) == 1) {
            return new ResultTo(ResultEnum.SUCCESS, "更新成功");
        } else {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "更新失败");
        }
    }

    /**
     * 删除下属机构
     *
     * @param id
     * @return
     */
    @ReqPermission(type = PermissionType.GROUP_INFO,opt = PermissOpt.DELETE)
    @PostMapping("/remove")
    public ResultTo remove(String id) {
        removeChild(id);
        if (DaoUtil.deleteById(adaptService, id, CmOrg.class) == 1) {
            return new ResultTo(ResultEnum.SUCCESS, "删除成功");
        } else {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "删除失败");
        }
    }

    private void removeChild(String parent) {
        Example example = new Example(CmOrg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId", parent);
        List<CmOrg> children = adaptService.selectByExample(example);
        for (CmOrg org : children) {
            DaoUtil.deleteById(adaptService, org.getId(), CmOrg.class);
            removeChild(org.getId());
        }
    }
}
